/*
 * @Descripttion:
 * @version: 1.0.0
 * @Author: liuzicheng
 * @Date: 2025-01-15 15:16:28
 * @LastEditors: liuzicheng
 * @LastEditTime: 2025-02-25 10:01:05
 */
/*
 * @Descripttion:
 * @version: 1.0.0
 * @Author: liuzicheng
 * @Date: 2025-01-15 15:16:28
 * @LastEditors: liuzicheng
 * @LastEditTime: 2025-01-15 16:58:16
 */
import react from "@vitejs/plugin-react";
import path from "path";
import Unocss from 'unocss/vite';
import { defineConfig, loadEnv } from "vite";
import { pluginIcons } from './build/plugin-isme/index';// https://vite.dev/config/
export default defineConfig(({ mode }) => {
	const env = loadEnv(mode, process.cwd(), "");
	// console.log(env.VITE_ENV, process.env);
	const reg = new RegExp(`^${env.VITE_APP_API_BASEURL}`);
	// 单独打包的依赖名单
	const moduleNames = { axios: ['axios'], react: ['react'], 'react-dom': ['react-dom'], antd: ['antd'], mobx: ['mobx'] }
	return {
		plugins: [react(), Unocss(),
		// // 自定义插件，用于生成页面文件的path，并添加到虚拟模块
		// pluginPagePathes(),
		// 自定义插件，用于生成自定义icon，并添加到虚拟模块
		pluginIcons(),
		
		],
		server: {
			port: 4000,
			[env.VITE_APP_API_BASEUR]: {
				target: env.VITE_APP_API_URL,
				changeOrigin: true,
				rewrite: (path: any) => path.replace(reg, ""),
			},
		},
		resolve: {
			alias: {
				'@': path.resolve(__dirname, './src'), // 配置 @ 为 src 的别名
			},
		},
		esbuild: {
			target: 'es2015',
		},
		css: {
			preprocessorOptions: {
				less: {
					javascriptEnabled: true,
					additionalData: `@import "@/assets/styles/global.less";`,
				},
			},
		},
		build: {
			target: 'es2015',
			chunkSizeWarningLimit: 1024, // 如果某个 chunk 大于 500KB，Vite 会警告
			minify: 'terser', // 使用 terser 压缩代码
			terserOptions: {
				compress: {
					drop_console: env.VITE_ENV != 'development', // 去除 console.log
					drop_debugger: env.VITE_ENV != 'development', // 去除 debugger
				},
				format: {
					/** 删除注释 */
					comments: false
				}
			},
			rollupOptions: {
				output: {
					manualChunks: {
						...moduleNames
					},
					chunkFileNames: 'js/[name]-[hash].js',
					entryFileNames: 'js/[name]-[hash].js',
					assetFileNames(id) {
						if (id.names[0].endsWith('.css')) {
							return 'css/[name]-[hash].[ext]'
						}
						if (id.names[0].endsWith('.svg')) {
							return 'img/[name]-[hash].[ext]'
						}
						return 'assets/[name]-[hash].[ext]'
					}
				}
			}
		}
	};
});
